﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;

namespace RadiantQCommunityControls
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            IList<CustomObject> list = this.GenerateData();
            // Always order by SortOrder - that's how we persist "sort order" after the end-user moves
            // rows around in this sample application.
            this.dataGrid1.ItemsSource = list.OrderBy(item => item.SortOrder).ToList();
        }

        void dataGrid1_RowsMoved(object sender, RowsMovedEventArgs args)
        {
            // args.StartIndex - The start index of the set of rows that are being moved
            // args.Count - The number of adjacent rows below the start index that are being moved.
            // args.DestinationIndex - The destination for this move

            // Interpreting this move varies by the application.
            // Here we will adjust the SortOrder of the bound objects and then rebind them with the grid.

            IList<CustomObject> list = this.dataGrid1.ItemsSource as IList<CustomObject>;
            this.MoveInList(list, args.StartIndex, args.Count, args.DestinationIndex);
        }

        private void dataGrid1_BeforeMovingRows(object sender, System.ComponentModel.CancelEventArgs e)
        {
            // Make selected items contiguous.
            this.FlattenSelection();
        }
        // There are 2 ways to handle this:
        // 1) Move items and adust their SortOrder (this is the technique used here).
        // 2) Another option is to just adjust the SortOrder and then rebind the DataGrid with a new list 
        //      that's properly sorted based on SortOrder.
        public int MoveInList(IList<CustomObject> list, int start, int count, int dest)
        {
            int from;
            int to;
            int newDestOfStart;
            
            if (dest > start)
            {
                from = start;
                to = dest - 1;
                for (int i = 0; i < count; i++)
                {
                    CustomObject view = list[start];

                    list.RemoveAt(start);
                    list.Insert(dest - 1, view);
                }
                newDestOfStart = dest - count;
            }
            else
            {
                from = dest;
                to = start + count - 1;
                for (int i = 0; i < count; i++)
                {
                    CustomObject view = list[start + i];
                    list.RemoveAt(start + i);
                    list.Insert(dest + i, view);
                }
                newDestOfStart = dest;
            }

            for (int i = from; i <= to; i++)
            {
                ((CustomObject)list[i]).SortOrder = i;
            }
            return newDestOfStart;
        
        }
        // If the user selected row 2 and row 5 then this method will also select row 3 and 4.
        // This is necessary because the logic here assumes that all selected objects are adjacent to each other.
        private void FlattenSelection()
        {
            int topRowIndex = Int32.MaxValue;
            int botRowIndex = Int32.MinValue;

            IList<CustomObject> list = this.dataGrid1.ItemsSource as IList<CustomObject>;
            foreach (CustomObject item in this.dataGrid1.SelectedItems)
            {
                int index = list.IndexOf(item);
                if (index < topRowIndex)
                    topRowIndex = index;
                if (index > botRowIndex)
                    botRowIndex = index;
            }
            if (this.dataGrid1.SelectedItems.Count < (botRowIndex - topRowIndex + 1))
            {
                for (int i = topRowIndex; i <= botRowIndex; i++)
                {
                    CustomObject o = list[i];
                    if (this.dataGrid1.SelectedItems.Contains(o) == false)
                        this.dataGrid1.SelectedItems.Add(o);
                }
            }
        }
        private IList<CustomObject> GenerateData()
        {
            List<CustomObject> list = new List<CustomObject>();
            Random rnd2, rnd3, rnd4, rnd5;
            rnd2 = new Random(DateTime.Now.Millisecond);
            rnd3 = new Random(DateTime.Now.Millisecond);
            rnd4 = new Random(DateTime.Now.Millisecond);
            rnd5 = new Random(DateTime.Now.Millisecond);

            for (int i = 0; i < 200; i++)
            {
                list.Add(new CustomObject() { SortOrder=i,
                                                OriginalIndex=i,
                                              Column2 = rnd2.Next().ToString(),
                                              Column3 = rnd3.Next().ToString(),
                                              Column4 = rnd4.Next().ToString(),
                                              Column5 = rnd5.Next().ToString(), 
                });
            }
            return list;
        }

    }
    public class CustomObject
    {
        public int SortOrder { get; set; }
        public int OriginalIndex { get; set; }
        public string Column2 { get; set; }
        public string Column3 { get; set; }
        public string Column4 { get; set; }
        public string Column5 { get; set; }
    }
}
